package com.example.myapplication.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.MvAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.Artist;
import com.example.myapplication.entity.Mv;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HotMVFragment extends Fragment {
    private View view;
    private RecyclerView recycle_view_mv;
    private Artist artist ;
    private List<Mv.MvsBean> mvList = new ArrayList<>();
    private MvAdapter adapter;

    public HotMVFragment() {

    }

    public static HotMVFragment newInstance(Bundle bundle) {
        HotMVFragment fragment = new HotMVFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mv, container, false);
        init();
        return view;
    }

    private void init() {
        recycle_view_mv = view.findViewById(R.id.recycle_view_mv);
        RecyclerView.LayoutManager manager = new GridLayoutManager(getContext(),2);
        recycle_view_mv.setLayoutManager(manager);
        adapter = new MvAdapter(getContext());
        recycle_view_mv.setAdapter(adapter);
        artist = (Artist) getArguments().getSerializable("singer");
        loadData(artist.getId());

    }
    private void loadData(int id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<Mv> call = service.getMv(id);
        call.enqueue(new Callback<Mv>() {
            @Override
            public void onResponse(Call<Mv> call, Response<Mv> response) {
                Mv baseResponse = response.body();
                mvList = baseResponse.getMvs();
                adapter.setData(mvList);
            }

            @Override
            public void onFailure(Call<Mv> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
